package com.szf.util;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.lang.exception.ExceptionUtils;

public abstract class ExceptionUtil extends ExceptionUtils {

	/**
	 * 将checked exception转换为unchecked exception.
	 * @param e 待转换的异常
	 * @return 转换后的unchecked异常
	 */
	public static RuntimeException convertCheckedExceptionToUnchecked(Exception e) {
		if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException
				|| e instanceof NoSuchMethodException)
			return new IllegalArgumentException(e.getMessage(), e);
		else if (e instanceof InvocationTargetException) {
			Throwable target = ((InvocationTargetException) e).getTargetException();
			if (target instanceof RuntimeException) {
				return (RuntimeException) target;
			}
			return new RuntimeException("Invocation Target Exception.", target);
		}
		else if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		}
		return new RuntimeException("Unexpected Checked Exception.", e);
	}

}
